﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CommonHelper;

namespace WebsitSetup
{
    public partial class SetServer : Form
    {
        /// <summary>
        /// 上一步的对象
        /// </summary>
        public SetupWizard setupWizard
        { get; set; }


        public SetServer()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="setupWizard">上一步的对象</param>
        public SetServer(SetupWizard setupWizard)
        {
            this.setupWizard = setupWizard;
            InitializeComponent();

            this.txtAddress.Text = CommonMethod.GetAddress();
            this.txtAddress.ReadOnly = true;

            this.txtName.Text = "Administrator";
            this.txtPass.Text = "123456";
        }

        /// <summary>
        /// 上一步
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLast_Click(object sender, EventArgs e)
        {
            this.setupWizard.Show();
            this.Hide();
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("您确定要退出吗？", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                Application.Exit();
            }
        }

        /// <summary>
        /// 下一步
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnNext_Click(object sender, EventArgs e)
        {
            SaveHost();
        }

        private void SaveHost()
        {
            var hostAddress = this.txtAddress.Text.Trim();
            var loadName = this.txtName.Text.Trim();
            var loadPass = this.txtPass.Text.Trim();

            if ( CommonMethod.checkString(hostAddress, loadName, loadPass))
            {
                try 
                {
                    ConfigHelper.SetHostValue(hostAddress, loadName, loadPass);
                    var createData = new CreateData(this);
                    createData.Show();
                    this.Hide();
                }
                catch(Exception ex)
                {
                    MessageBox.Show("设置主机用户名密码时出错，原因为：" + ex.Message);
                }
            }
            else
            {
                MessageBox.Show("地址、用户名、密码都不能为空.");
            }
        }
    }
}
